package com.szhuang.mobilesafe.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.szhuang.mobilesafe.R;
import com.szhuang.mobilesafe.ui.SettingItemView;

/**
 * Created by szhuang on 2015/3/23.
 */
public class SetUpThirdPage extends GestureDetectorActivity {

    ImageView last,next;
    private EditText contacts;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_third_page);
        init();
        setListener();
    }



    public void init(){

        sp=getSharedPreferences("config",MODE_PRIVATE);
        contacts=(EditText)findViewById(R.id.contacts);
        last=(ImageView)findViewById(R.id.backward);
        next=(ImageView)findViewById(R.id.forward);

        contacts.setText(sp.getString("safephone",""));
    }

    public void setListener(){



        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone=contacts.getText().toString().trim();
                SharedPreferences.Editor editor=sp.edit();
                editor.putString("safephone",phone);
                editor.commit();
                previousStep();
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone=contacts.getText().toString().trim();
                if(TextUtils.isEmpty(phone)){
                    Toast.makeText(SetUpThirdPage.this, "安全号码还没有设置", Toast.LENGTH_SHORT).show();
                }else{

                    SharedPreferences.Editor editor=sp.edit();
                    editor.putString("safephone",phone);
                    editor.commit();
                    nextStep();
                }

            }
        });
    }

    @Override
    public void previousStep() {


        Intent intent=new Intent(SetUpThirdPage.this,SetUpSecondPage.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.tran_pre_in, R.anim.tran_pre_out);
    }

    @Override
    public void nextStep() {

        Intent intent=new Intent(SetUpThirdPage.this,SetUpFourthPage.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
    }

    public void selectContacts(){

        Intent intent=new Intent(SetUpThirdPage.this,ContactsActivity.class);
        startActivityForResult(intent,0);
    }

    public void selectContacts(View view){
        Intent intent=new Intent(SetUpThirdPage.this,ContactsActivity.class);
        startActivityForResult(intent,0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data==null){
            return;
        }
        String phone=data.getStringExtra("phone").replace("-","");
        contacts.setText(phone);
    }
}
